package org.aiforum.backend.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.aiforum.backend.service.AdminService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;


public class AiBackendListener implements ServletContextListener {
	
	private Logger logger=LogManager.getLogger(getClass());

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void contextInitialized(ServletContextEvent event) {
		logger.info("Start ai backend listener");
		// 获取Servlet上下文对象
		ServletContext servletContext = event.getServletContext();
		// 获取Spring框架上下文对象
		WebApplicationContext springContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
		
		// 从Spring上下文获取管理员业务接口
		AdminService adminService = springContext.getBean(AdminService.class);
		// 初始化超级管理员
		adminService.initSuperAdmin();
			
	}



}
